package com.itboye.sunsunhome.www.aq;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.itbboye.adapter.AddDeviceAdapter;
import com.itboye.base.BaseActivity;
import com.itboye.sunsunhome.util.AddBenas;
import com.itboye.sunsunhome.util.CameraPopupWindow;
import com.itboye.sunsunhome.www.aq.base.AqDeviceInfo;
import com.itboye.sunsunhome.www.aq.base.MyApplication;

/**
 * 日期：2015-04-30 1. 添加版本识别功能，支持AQ107、AQ209和AQ805设备 日期：2015-05-05 1. 添加手动添加设备功能
 */
@SuppressLint("NewApi")
public class AddDeviceNewActivity extends BaseActivity implements
		OnClickListener {
	int count = 0;
	MyApplication mApp;
	boolean mReadyExit;
	ListView mListView;
	Context mContext;
	AqDeviceInfo mSelectDeviceInfo;

	private TextView title;
	private ImageView imgAdd;
	private ListView listview;
	ImageView adImg;
	AddDeviceAdapter adapter;
	List<AddBenas> benas;
	ImageView back;

	String name[] = { "水族箱智能监控系统", "池塘智能监控系统", "智能水族灯", "远程智能加热棒", "智能变频水泵",
			"水产养殖设备" };
	int url[] = { R.drawable.divce001, R.drawable.device_002,
			R.drawable.device_003, R.drawable.device_004,
			R.drawable.device_005, R.drawable.device_006 };
	int add[] = { R.drawable.add_device, R.drawable.add_device,
			R.drawable.add_device, R.drawable.add_device,
			R.drawable.add_device, R.drawable.add_device };

	PopupWindow popupWindow;
	CameraPopupWindow cameraPopupWindow;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_add_news_device);
		mApp = (MyApplication) getApplication();
		adImg = (ImageView) findViewById(R.id.adImg);
		listview = (ListView) findViewById(R.id.addListview);
		back=(ImageView) findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		// getAdImage();
		mContext = this;
		mApp.addDeviceUI = this;
		iniText();
		onAdapter();
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if (position == 0) {
//					showPopwindow();
					
					cameraPopupWindow = new CameraPopupWindow(
							AddDeviceNewActivity.this, onclicklister);
					cameraPopupWindow.showAtLocation(view, Gravity.BOTTOM
							| Gravity.CENTER_HORIZONTAL, 0, 0);
				} else if (position == 1) {
					Toast.makeText(AddDeviceNewActivity.this, "该功能还未开放哦！",
							Toast.LENGTH_SHORT).show();
				} else if (position == 2) {
					Toast.makeText(AddDeviceNewActivity.this, "该功能还未开放哦！",
							Toast.LENGTH_SHORT).show();
				} else if (position == 3) {
					Toast.makeText(AddDeviceNewActivity.this, "该功能还未开放哦！",
							Toast.LENGTH_SHORT).show();
				} else if (position == 4) {
					Toast.makeText(AddDeviceNewActivity.this, "该功能还未开放哦！",
							Toast.LENGTH_SHORT).show();
				} else if (position == 5) {
					Toast.makeText(AddDeviceNewActivity.this, "该功能还未开放哦！",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		IntentFilter intentFilterAvart = new IntentFilter("finish");
		registerReceiver(avartBroadcastReceiver, intentFilterAvart);

	}
	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			finish();
			System.out.println("zhixingle");
		};
	};
	private OnClickListener onclicklister = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v.getId()==R.id.open_configuration){
				Intent mainIntent = new Intent(AddDeviceNewActivity.this,
						MDeviceActivity.class);
				startActivity(mainIntent);
				cameraPopupWindow.dismiss();
			}else if(v.getId()==R.id.open_camera){
				// 添加局域网设备
				Intent mainIntent = new Intent(AddDeviceNewActivity.this,
						AddDeviceActivity.class);
				startActivity(mainIntent);
			}else if(v.getId()==R.id.pick_image){
				Intent mainIntent = new Intent(AddDeviceNewActivity.this,
						ManualAddDeviceActivity.class);
				startActivity(mainIntent);
			}else if(v.getId()==R.id.camera_cancel_tv){
				cameraPopupWindow.dismiss();
			}
			
		}
	};
	public void iniText() {

		imgAdd = (ImageView) findViewById(R.id.imgAdd);
		title = (TextView) findViewById(R.id.title);
		title.setVisibility(View.GONE);
		imgAdd.setVisibility(View.GONE);

	}

	/*private void showPopwindow() {
//		View parent = ((ViewGroup) this.findViewById(android.R.id.addListview))
//				.getChildAt(0);
		LayoutInflater layoutInflater = (LayoutInflater) MyApplication.context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View popView = layoutInflater.inflate(R.layout.add_menu_windss, null);


		TextView open_configuration = (TextView) popView
				.findViewById(R.id.open_configuration);
		TextView open_camera = (TextView) popView
				.findViewById(R.id.open_camera);
		TextView pick_image = (TextView) popView.findViewById(R.id.pick_image);
		TextView camera_cancel_tv = (TextView) popView
				.findViewById(R.id.camera_cancel_tv);

		int width = getResources().getDisplayMetrics().widthPixels;
		int height = getResources().getDisplayMetrics().heightPixels;

		final PopupWindow popWindow = new PopupWindow(popView, width, height);
		popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		// popWindow.setAnimationStyle(R.style.AnimBottom);
		popWindow.setFocusable(true);
		popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
		//防止被虚拟键挡住
		popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

		

		open_configuration.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
						
						Intent mainIntent = new Intent(AddDeviceNewActivity.this,
								MDeviceActivity.class);
						startActivity(mainIntent);
			}
		});
		open_camera.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 添加局域网设备
				Intent mainIntent = new Intent(AddDeviceNewActivity.this,
						AddDeviceActivity.class);
				startActivity(mainIntent);
			}
		});
		pick_image.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 手动添加设备
		
				
				
				Intent mainIntent = new Intent(AddDeviceNewActivity.this,
						ManualAddDeviceActivity.class);
				startActivity(mainIntent);
//				Intent mainIntent = new Intent(AddDeviceNewActivity.this,
//						ManualAddDeviceActivity.class);
//				startActivity(mainIntent);
			}
		});
		camera_cancel_tv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popWindow.dismiss();
			}
		});

		ColorDrawable dw = new ColorDrawable(0x30000000);
		popWindow.setBackgroundDrawable(dw);
		popWindow.showAtLocation(popView, Gravity.BOTTOM
				| Gravity.CENTER_HORIZONTAL, 0, 0);
		
	}
	*/
	@SuppressWarnings("unused")
	public void onAdapter() {
		benas = new ArrayList<AddBenas>();
		for (int i = 0; i < name.length; i++) {
			AddBenas addBenas = new AddBenas();
			addBenas.setName(name[i]);
			addBenas.setBitmp(add[i]);
			addBenas.setImg(url[i]);
			benas.add(addBenas);
		}
		adapter = new AddDeviceAdapter(benas, mApp.addDeviceUI);
		listview.setAdapter(adapter);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.device_action_settings1) {
			// 一键配置
			Intent mainIntent = new Intent(AddDeviceNewActivity.this,
					SmartConfigActivity.class);
			AddDeviceNewActivity.this.startActivity(mainIntent);
			return true;
		}
		if (id == R.id.device_action_settings2) {
			// 添加局域网设备
			Intent mainIntent = new Intent(AddDeviceNewActivity.this,
					AddDeviceActivity.class);
			AddDeviceNewActivity.this.startActivity(mainIntent);
			return true;
		}
		if (id == R.id.device_action_settings3) {
			// 手动添加设备
			Intent mainIntent = new Intent(AddDeviceNewActivity.this,
					ManualAddDeviceActivity.class);
			AddDeviceNewActivity.this.startActivity(mainIntent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
